package hot100.logos31To40;

import hot100.datastruct.TreeNode;

/**
 * @author logos
 * date 2025/7/14 11:44
 * @version v1.0
 * @Package hot100.logos31To40
 */
public class logos40 {

    /**
     * 40.二叉树的直径
     * https://leetcode.cn/problems/diameter-of-binary-tree/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution {
        private int maxLen;

        public int diameterOfBinaryTree(TreeNode root) {
            maxLen = 0;
            depth(root);
            return maxLen;
        }

        private int depth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int l = depth(root.left);
            int r = depth(root.right);
            maxLen = Math.max(maxLen, l + r);
            return Math.max(l, r) + 1;
        }
    }
}
